#include "homepage.h"
#include "ui_homepage.h"
#include "globalval.h"
#include <QPixmap>
#include <QSqlQuery>
HomePage::HomePage(QWidget *parent,QSqlDatabase db) :
    QWidget(parent),
    ui(new Ui::HomePage),
    db(db)
{
    ui->setupUi(this);


    //实例化四个界面
    m0Wig = new M0Wig;
    modbusWig = new ModbusWig;
    mainwindow = new MainWindow;
    setWig = new SetWig;
    // brokenLineWig = new BrokenLineWig; // 实例化 BrokenLineWig

    this->setFixedSize(720,450);
    m0Wig->setFixedSize(600,450);
    modbusWig->setFixedSize(600,450);
    mainwindow->setFixedSize(600,450);
    setWig->setFixedSize(600,450);
    //将四个界面依次添加到堆栈窗体
    ui->stackedWidget->addWidget(m0Wig);
    ui->stackedWidget->addWidget(modbusWig);
    ui->stackedWidget->addWidget(mainwindow);
    ui->stackedWidget->addWidget(setWig);
    this->setWindowIcon(QIcon(":/new/prefix1/pic/smartHome.png"));

    //连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));

    //mqtt相关
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress("192.168.1.128"));//输入mqtt服务器ip
    mqtt->setPort(1883);
    mqtt->connectToHost();




    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
    //    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
    //                     modbusWig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),m0Wig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),modbusWig, SLOT(dataUpSlot(QByteArray)));
    //QObject::connect(mqtt, SIGNAL(ctrlUpSignal(QByteArray)),m0Wig, SLOT(dataCtrlSlot(QByteArray)));

    // 连接信号与槽
    //QObject::connect(m0Wig, SIGNAL(historyDataReceived(int, const QList<QPair<qint64, QString>>&)),
    //brokenLineWig, SLOT(receiveHistoryData(int, const QList<QPair<qint64, QString>>&)));


}

void HomePage::setvalue(QString &value)
{
    id = value;
    qDebug() << id;
    ui->idlabel->setText(id);
    QString selectId = id;
//    qDebug() << ui->idlabel->text();
//    qDebug() << id;
    // 从数据库中根据selectId获取头像路径
       if (db.isOpen()) {
           QSqlQuery query(db);
           query.prepare("SELECT avatar_path FROM login WHERE id = :id");
           query.bindValue(":id", selectId);

           if (query.exec() && query.next()) {
               QString avatarPath = query.value(0).toString();
               if (!avatarPath.isEmpty()) {
                   QPixmap pixmap(avatarPath);
                   if (!pixmap.isNull()) {
                       // 将图片缩放到 avaLabel 的大小
                       pixmap = pixmap.scaled(
                           ui->avalabel->size(),
                           Qt::KeepAspectRatio,
                           Qt::SmoothTransformation
                       );
                       // 在 avaLabel 上显示图片
                       ui->avalabel->setPixmap(pixmap);
                   } else {
                       qDebug() << "无法加载头像图片:" << avatarPath;
                   }
               } else {
                   qDebug() << "头像路径为空";
               }
           } else {
               qDebug() << "查询头像路径失败或无结果";
           }
       } else {
           qDebug() << "数据库未打开，无法获取头像路径";
       }
}

HomePage::~HomePage()
{
    delete ui;
}
